<html>
  <head>
    <title></title>
  </head>
  <body>
    <script>
      var current_test = 0;
      var test_list = [
        getAVCodecs,
        getCPUInfo,
        getDisplayInfo,
        getMemoryInfo,
        getStorageInfo,
        endTest
      ];

      function runNextTest() {
        test_list[current_test++]();
      };

      function reportFail(message) {
        console.log(message);
        document.title = "Fail";
      };

      function endTest() {
        document.title = "Pass";
      };

      var api = xwalk.experimental.system;

      function getAVCodecs() {
        function checkAVCodecsInfo(info) {
          if (!info.audioCodecs)
            reportFail("Missing AudioCodec attribute in the SystemAVCodecs object.");

          if (!info.videoCodecs)
            reportFail("Missing VideoCodec name in the SystemAVCodecs object.");

          for (var i = 0; i < info.audioCodecs.length; ++i) {
            if (info.audioCodecs[i].format == null)
              reportFail("Missing audio encoding format in the AudioCodec object.");

            if (typeof info.audioCodecs[i].format != "string")
              reportFail("Audio encoding format is not a string.");
          }

          for (var i = 0; i < info.videoCodecs.length; ++i) {
            if (info.videoCodecs[i].format == null)
              reportFail("Missing video encoding format in the VideoCodec object.");

            if (typeof info.videoCodecs[i].format != "string")
              reportFail("Video encoding format is not a string.");

            if (info.videoCodecs[i].hwAccel == null)
              reportFail("Missing hwAccel in the VideoCodec object.");

            if (typeof info.videoCodecs[i].hwAccel != "boolean")
              reportFail("hwAccel is not a boolean.");

            if (info.videoCodecs[i].encode == null)
              reportFail("Missing encode in the VideoCodec object.");

            if (typeof info.videoCodecs[i].encode != "boolean")
              reportFail("encode is not a boolean.");
          }

          if (document.title != "Fail")
            runNextTest();
        };

        api.getAVCodecs().then(checkAVCodecsInfo);
      };

      function getCPUInfo() {
        var call_max = 1000;
        var call_count = 0;

        function checkCPUInfo(info) {
          if (info.numOfProcessors < 1)
            reportFail("What!? A computer with less than 1 CPU?");

          if (!info.archName)
            reportFail("Missing archtecture name in the SystemCPU object.");

          if (typeof info.archName != "string")
            reportFail("Archtecture name is not a string.");

          if (info.load < 0 || info.load > 1)
            reportFail("Load should be in the range of 0 and 1.");

          if (++call_count == call_max)
            runNextTest();
        };

        for (var i = 0; i < call_max; ++i)
          api.getCPUInfo().then(checkCPUInfo, reportFail);
      };

      function getDisplayInfo() {
        function checkDisplayInfo(info) {
          for (var i = 0; i < info.displays.length; ++i) {
            if (typeof info.displays[i].id != "string")
              reportFail("Display id is not a string.");

            if (info.displays[i].id.length == 0)
              reportFail("Display id can't be empty.");

            if (typeof info.displays[i].name != "string")
              reportFail("Display name is not a string.");

            if (typeof info.displays[i].primary != "boolean")
              reportFail("Display type is not a boolean.");

            if (typeof info.displays[i].external != "boolean")
              reportFail("Display type is not a boolean.");

            if (typeof info.displays[i].deviceXDPI != "number")
              reportFail("Display deviceXDPI must be a number.");

            if (typeof info.displays[i].deviceYDPI != "number")
              reportFail("Display deviceYDPI must be a number.");

            if (typeof info.displays[i].width != "number")
              reportFail("Display width must be a number.");

            if (typeof info.displays[i].height != "number")
              reportFail("Display height must be a number.");

            if (typeof info.displays[i].availWidth != "number")
              reportFail("Display availWidth must be a number.");

            if (typeof info.displays[i].availHeight != "number")
              reportFail("Display availHeight must be a number.");

            if (typeof info.displays[i].colorDepth != "number")
              reportFail("Display colorDepth must be a number.");

            if (typeof info.displays[i].pixelDepth != "number")
              reportFail("Display pixelDepth must be a number.");
          }

          if (document.title != "Fail")
            runNextTest();
        };

        api.getDisplayInfo().then(checkDisplayInfo);
      };

      function getMemoryInfo() {
        function checkMemoryInfo(info) {
          if (info.capacity <= 0)
            reportFail("How can we run this in a computer with no memory?");

          if (info.availCapacity <= 0)
            reportFail("You should have some free memory to run this test.");

          if (info.availCapacity > info.capacity)
            reportFail("Availiable memory cannot be more than the total memory.");

          if (document.title != "Fail")
            runNextTest();
        };

        api.getMemoryInfo().then(checkMemoryInfo, reportFail);
      };

      function getStorageInfo() {
        function checkStorageInfo(info) {
          if (!info.storages)
            reportFail("We must have at least 1 storage device.");

          for (var i = 0; i < info.storages.length; ++i) {
            if (typeof info.storages[i].id != "string")
              reportFail("Storage id is not a string.");

            if (info.storages[i].id.length == 0)
              reportFail("Storage id can't be empty.");

            if (typeof info.storages[i].name != "string")
              reportFail("Storage name is not a string.");

            if (typeof info.storages[i].type != "string")
              reportFail("Storage type is not a string.");

            if (info.storages[i].type.length == 0)
              reportFail("Storage type can't be empty.");

            if (typeof info.storages[i].capacity != "number")
              reportFail("Storage capacity must be a number.");

            if (info.storages[i].capacty <= 0)
              reportFail("Storage has to be able to store something.");
          }

          if (document.title != "Fail")
            runNextTest();
        };

        api.getStorageInfo().then(checkStorageInfo);
      };

      runNextTest();
    </script>
  </body>
</html>
